<template>
  <el-form ref="basicFormRef" :model="userInfo" :rules="rules" label-width="80px">
    <el-form-item label="个性签名" prop="nickname">
      <el-tooltip class="item" effect="dark" content="秀一秀你的文采啦~" placement="top-start">
        <el-input v-model="userInfo.note" maxlength="30"/>
      </el-tooltip>
    </el-form-item>
    <el-form-item label="用户昵称" prop="nickname">
      <el-tooltip class="item" effect="dark" content="换一个猛一点的,唬住他们~" placement="top-start">
        <el-input v-model="userInfo.nickname" maxlength="20"/>
      </el-tooltip>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="userInfo.email" maxlength="30"/>
    </el-form-item>
    <el-form-item label="性别">
      <el-radio-group v-model="userInfo.gender">
        <el-radio :label="0">男</el-radio>
        <el-radio :label="1">女</el-radio>
        <el-radio :label="2">未知</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">保存</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { reactive, toRefs, ref } from 'vue'
import memberApi from '../../../api/member'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

export default {
  name: 'basicInfo',
  props: {
    userInfo: {
      type: Object
    }
  },
  setup (props) {
    const router = useRouter()
    const state = reactive({
      // 表单校验
      rules: {
        nickname: [
          { required: true, message: '用户昵称不能为空', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱地址不能为空', trigger: 'blur' },
          {
            type: 'email',
            message: '请输入正确的邮箱地址',
            trigger: ['blur', 'change']
          }
        ],
      }
    })
    const basicFormRef = ref(null)
    const store = useStore()
    //------------------- method start
    // 提交表单
    const submit = () => {
      basicFormRef.value.validate(valid => {
        if (valid) {
          memberApi.updateMemberBasicInfo({
            note: props.userInfo.note,
            nickname: props.userInfo.nickname,
            email: props.userInfo.email,
            gender: props.userInfo.gender,
          }).then(res => {
            store.dispatch('GetInfo').then(() => {
              ElMessage.success('修改成功')
            })
          })
        }
      })
    }
    // 关闭
    const close = () => {
      router.push('/home')
    }
    //------------------- method end


    return {
      ...toRefs(state),
      submit,
      close,
      basicFormRef
    }

  }
}
</script>

<style scoped>

</style>
